package cn.edu.hubu.hubuhotel.controller;


import cn.edu.hubu.hubuhotel.entity.User;
import cn.edu.hubu.hubuhotel.service.UserService;
import cn.edu.hubu.hubuhotel.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    //判断账号是否存在
    @RequestMapping("/check")
    public ResultVO check(String account){
        return userService.checkAccount(account);
    }
    @RequestMapping("/register")
    public ResultVO register(User user){
        System.out.println("~~~~~~~register");
        ResultVO vo = userService.saveUser(user);
        return vo;
    }
    //检查明文
    @RequestMapping("/checkLogin")
    public ResultVO checkLogin(String account,String password){
        System.out.println("~~~~~~~~~~checkLogin");
        return userService.checkLogin(account,password);
    }

    //检查类型
    //检查密文
    @RequestMapping("/loginPlus")
    public ResultVO loginPlus(String account,String password,int typeId){
        System.out.println("~~~~~~~loginPlus");
        ResultVO vo = userService.checkLogin3(account, password,typeId);
        return vo;
    }


    //  根据电话查询
    @GetMapping("/select/{phone}")
    public ResultVO<User> selectUser(@PathVariable String phone) {
        System.out.println("~~~~~~~selectUser");
        return userService.searchUser(phone);
    }

    // 根据用户类型查询用户信息（切换网页中表格）
    @GetMapping("/type/{usertype}")
    public ResultVO<List<User>> divideUser(@PathVariable int usertype) {
        System.out.println("~~~~~~~divideUser");
        return userService.divideUser(usertype);
    }

    // 添加用户
    @PostMapping("/add")
    public ResultVO<String> increaseUser(@RequestBody User user) {
        System.out.println("~~~~~~~increaseUser");
        return userService.increaseUser(user);
    }

    // 更新用户信息
    @PutMapping("/update")
    public ResultVO<String> reviseUser(@RequestBody User user) {
        System.out.println("~~~~~~~reviseUser");
        return userService.reviseUser(user);
    }

    @DeleteMapping("/delete")
    public ResultVO<String> cancleUser(@RequestBody Map<String, Integer> payload) {
        int id = payload.get("id");
        System.out.println("~~~~~~~cancleUser");
        return userService.cancleUser(id);
    }


    @RequestMapping("/changeUser")
    public ResultVO<String> updateUser(User user) {
        System.out.println("~~~~~~~updateUser");
        return userService.updateUser(user);
    }
}
